package com.example.utils.treeutils;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.model.DeptModel;
import com.example.auth.model.MenuModel;
import com.example.model.TreeModel;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname TreeUtils
 * @Description TODO
 * @Date 2022/7/31 9:10
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class TreeUtils {

    /**
     * 获取树形组件
     * @param treeModelList
     * @param pid
     * @return
     */
    public static List<TreeModel> getTreeModelList(List<TreeModel> treeModelList,Integer pid){
        List<TreeModel> trees=treeModelList.stream().filter(e->{
            return  e.getPid().equals(pid);
        }).map((m)->{
            m.setChildren(getChilds(treeModelList,m));
            return m;
        }).collect(Collectors.toList());
        return trees;
    }
    /**
     * 处理树结构
     * @param
     * @return
     */
    private static List<TreeModel> getChilds(List<TreeModel> treeModelList, TreeModel treeModel){
        List<TreeModel> trees=treeModelList.stream().filter(e->{
            return Objects.equals(e.getPid(),treeModel.getId());
        }).map(
                (m)->{
                    m.setChildren(getChilds(treeModelList,m));
                    return m;
                }
        ).collect(Collectors.toList());
        return trees;
    }


}
